package com.xbongbong.saas.model.impl;

import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.enums.DelEnum;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.CollectionsUtil;
import com.xbongbong.paas.toolbox.util.DateTimeUtil;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.saas.constant.BasicConstant;
import com.xbongbong.saas.constant.ParameterConstant;
import com.xbongbong.saas.constant.StringConstant;
import com.xbongbong.saas.domain.dao.AssembleProductDao;
import com.xbongbong.saas.domain.entity.AssembleProductEntity;
import com.xbongbong.saas.domain.entity.ext.ProductEntityExt;
import com.xbongbong.saas.enums.business.ProductEnum;
import com.xbongbong.saas.model.AssembleProductModel;
import com.xbongbong.saas.model.ProductModel;
import com.xbongbong.saas.util.StandardizeDataUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 吴峰
 * @version v1.0
 * @date 2019/2/18 15:29
 * @since v1.0
 */
@Service("AssembleProductModel")
public class AssembleProductModelImpl implements AssembleProductModel{
    @Resource
    private AssembleProductDao assembleProductDao;
    @Resource
    private ProductModel productModel;
    /**
     * 插入数据
     *
     * @param assembleProduct 实体数据
     * @return Integer
     * @author Jian.Wang
     * @date 2019/2/23 16:51
     * @since v1.0
     **/
    @Override
    public Integer insert(AssembleProductEntity assembleProduct) {
        assembleProduct.setAddTime(DateTimeUtil.getInt());
        assembleProduct.setUpdateTime(DateTimeUtil.getInt());
        StandardizeDataUtil.standardizeNotString(assembleProduct);
        return assembleProductDao.insert(assembleProduct);
    }

    /**
     * 更新数据
     *
     * @param assembleProduct 实体数据
     * @return Integer
     * @author Jian.Wang
     * @date 2019/2/23 16:51
     * @since v1.0
     **/
    @Override
    public Integer update(AssembleProductEntity assembleProduct) {
        assembleProduct.setUpdateTime(DateTimeUtil.getInt());
        StandardizeDataUtil.standardizeNotString(assembleProduct);
        return assembleProductDao.update(assembleProduct);
    }
    /**
     * 保存数据
     *
     * @param assembleProduct 实体数据
     * @return Integer
     * @author Jian.Wang
     * @date 2019/2/23 16:51
     * @since v1.0
     **/
    @Override
    public Integer save(AssembleProductEntity assembleProduct) {
        if (assembleProduct.getId() == null || assembleProduct.getId().equals(0L)) {
            return assembleProductDao.insert(assembleProduct);
        }
        return assembleProductDao.update(assembleProduct);
    }

    @Override
    public Integer insertBatch(List<AssembleProductEntity> list) {
        if (CollectionsUtil.isEmpty(list)) {
            return 0;
        }
        StandardizeDataUtil.standardize4AssembleProductEntityList(list);
        return assembleProductDao.insertBatch(list);
    }

    /**
     * 获取装配单关联产品
     *
     * @param id     id
     * @param corpid 公司id
     * @return 产品
     */
    @Override
    public List<AssembleProductEntity> getProductsByAssembleId(Long id, String corpid) {
        // 获取关联产品
        Map<String, Object> modelMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        modelMap.put(ParameterConstant.CORPID, corpid);
        modelMap.put("assembleId", id);
        modelMap.put(ParameterConstant.ORDER_BY_STR, StringConstant.SAAS_LINK_BUSINESS_ID);
        modelMap.put(ParameterConstant.DEL, DelEnum.NORMAL.getDel());
        return findEntitys(modelMap);
    }

    @Override
    public List<AssembleProductEntity> getProductsByAssembleId(Long id, String corpid, Integer type, List<Integer> delIn) {
        // 获取关联产品
        Map<String, Object> modelMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        modelMap.put(ParameterConstant.CORPID, corpid);
        modelMap.put("assembleId", id);
        modelMap.put("type", type);

        modelMap.put("delIn", delIn);
        return findEntitys(modelMap);
    }

    @Override
    public List<AssembleProductEntity> getProductsByAssembleId(Long id, String corpid, List<Integer> delIn) {
        // 获取关联产品
        Map<String, Object> modelMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        modelMap.put(ParameterConstant.CORPID, corpid);
        modelMap.put("assembleId", id);
        modelMap.put("delIn", delIn);
        modelMap.put(ParameterConstant.ORDER_BY_STR, BasicConstant.SORT);
        return findEntitys(modelMap);
    }

    @Override
    public Long isProductSubAttrExist(String attr, String corpid, Integer type, Integer isArray) {
        return assembleProductDao.isProductSubAttrExist(attr,corpid,type,isArray);
    }

    @Override
    public Double summaryByAttr(String field,String corpid, Long refId, List<Long> productIdIn,Integer type){
        return assembleProductDao.summaryByAttr(field,corpid,  refId, productIdIn,type);
    }

    @Override
    public Double summaryByAttrFromOutSide(String field, String corpid, Long refId, List<Long> productIdIn, Integer type) {
        return assembleProductDao.summaryByAttrFromOutSide(field,corpid,  refId, productIdIn,type);
    }

    @Override
    public Long countProductSub(String attr, String corpid, Integer type, Integer isArray) {
        return assembleProductDao.countProductSub(attr, corpid, type, isArray);
    }

    @Override
    public List<AssembleProductEntity> getProductWithSubData(String attr, String corpid, Integer type, Integer isArray, Long idGte, Integer limit) {
        return assembleProductDao.getProductWithSubData(attr, corpid, type, isArray, idGte, limit);
    }

    /**
     * 删除单条数据
     *
     * @param key    主键
     * @param corpid 公司ID
     * @return Integer
     * @author Jian.Wang
     * @date 2019/2/23 16:51
     * @since v1.0
     **/
    @Override
    public Integer deleteByKey(Long key, String corpid) {
        return assembleProductDao.deleteByKey(key,corpid);
    }

    /**
     * 根据关联对象id和公司id删除数据
     *
     * @param refId  关联对象ID集合
     * @param corpid 公司ID
     * @return Integer
     * @author Jian.Wang
     * @date 2019/2/23 16:51
     * @since v1.0
     **/
    @Override
    public Integer deleteByAssembleId(Long refId, String corpid) {
        return assembleProductDao.deleteByKey(refId,corpid);
    }

    /**
     * 根据关联对象id集合和公司id删除数据
     *
     * @param refIdIn 关联对象ID集合
     * @param corpid  公司ID
     * @return Integer
     * @author Jian.Wang
     * @date 2019/2/23 16:51
     * @since v1.0
     **/
    @Override
    public Integer deleteByAssembleIdIn(List<Long> refIdIn, String corpid) {
        return assembleProductDao.deleteByAssembleIdIn(refIdIn,corpid);
    }

    /**
     * 根据索引查询数据
     *
     * @param key    主键
     * @param corpid 公司ID
     * @return AssembleProductEntity
     * @author Jian.Wang
     * @date 2019/2/23 16:51
     * @since v1.0
     **/
    @Override
    public AssembleProductEntity getByKey(Long key, String corpid) {
        return assembleProductDao.getByKey(key,corpid);
    }

    /**
     * 根据索引查询数据
     *
     * @param param 查询参数
     * @return List<AssembleProductEntity>
     * @author Jian.Wang
     * @date 2019/2/23 16:51
     * @since v1.0
     **/
    @Override
    public List<AssembleProductEntity> findEntitys(Map<String, Object> param) {
        return assembleProductDao.findEntitys(param);
    }

    /**
     * 模糊查询装配单IDs
     *
     * @param corpid 公司Id
     * @param nameOrNoLike 查询参数
     * @param type 类型
     * @return List<Long>
     * @author Jian.Wang
     * @date 2019/2/23 16:51
     * @since v1.0
     **/
    @Override
    public List<Long> findAssembleIdsByRelaProNameOrNo(String corpid, String nameOrNoLike, Integer type) {
        Map<String, Object> param = new HashMap<>();
        param.put("corpid", corpid);
        param.put("nameOrNoLike", nameOrNoLike);
        param.put("del", 0);
        param.put("type", type);
        return assembleProductDao.findAssembleIdsByRelaProNameOrNo(param);
    }

    /**
     * 关联查询
     *
     * @param param 参数集合
     * @return List<AssembleProductEntity>
     * @author Jian.Wang
     * @date 2019/2/23 16:51
     * @since v1.0
     */
    @Override
    public List<AssembleProductEntity> findEntitysJoinProduct(Map<String, Object> param) {
        return assembleProductDao.findEntitysJoinProduct(param);
    }

    /**
     * 获取数据条数
     *
     * @param param 查询参数
     * @return Integer
     * @author Jian.Wang
     * @date 2019/2/23 16:51
     * @since v1.0
     **/
    @Override
    public Integer getEntitysCount(Map<String, Object> param) {
        return assembleProductDao.getEntitysCount(param);
    }

    @Override
    public List findEntitysWithoutSub(Map<String, Object> param) {
        return null;
    }

    /**
     * 获取装配单产品
     *
     * @param assembleIds 装配单ids
     * @param corpid      公司id
     * @return 根据产品表更新相关信息后的装配单产品
     * @author 周智
     * 创建时间： 2017-12-28 10:36
     */
    public List<AssembleProductEntity> getAssembleProducts(List<Integer> assembleIds, String corpid) throws XbbException {
        Map<String, Object> param = new HashMap<>(16);
        param.put("assembleIdIn", assembleIds);
        param.put("corpid", corpid);
        param.put("del", 0);
        List<AssembleProductEntity> assembleProductList = findEntitys(param);

        List<Long> productIds = new ArrayList<>();
        productIds.add(-1L);
        for (AssembleProductEntity assembleProduct : assembleProductList) {
            productIds.add(assembleProduct.getProductId());
        }
        param.clear();
        param.put("productIdIn", productIds);
        param.put("corpid", corpid);
        List<ProductEntityExt> productList = productModel.findEntitys(param);

        Map<Long, ProductEntityExt> productMap = new HashMap<>(16);
        for (ProductEntityExt product : productList) {
            productMap.put(product.getId(), product);
        }

        for (AssembleProductEntity assembleProduct : assembleProductList) {
            ProductEntityExt product = productMap.get(assembleProduct.getProductId());
            if (product != null) {
                JSONObject dataList = product.getData();
                if (null == dataList) {
                    throw new XbbException(SystemErrorCodeEnum.API_ERROR_100002);
                }
                assembleProduct.setProductName(dataList.getString(ProductEnum.NAME.getAttr()));
                assembleProduct.setProductSpecification(dataList.getString(ProductEnum.SPECIFICATION.getAttr()));
                assembleProduct.setProductUnit(dataList.getString(ProductEnum.UNIT.getAttr()));
                assembleProduct.setCategoryId(dataList.getInteger(ProductEnum.CATEGORY_ID.getAttr()));
                assembleProduct.setImageUrl(dataList.getString(ProductEnum.THUMBNAIL.getAttr()));
                assembleProduct.setBarcode(dataList.getString(ProductEnum.BARCODE.getAttr()));
            }
        }
        return assembleProductList;
    }

    @Override
    public Integer updateBatch(List<AssembleProductEntity> list, String corpid) {
        if (CollectionsUtil.isEmpty(list)) {
            return 0;
        }
        long now = DateTimeUtil.getInt();
        for (AssembleProductEntity entity : list) {
            entity.setUpdateTime(now);
        }
        StandardizeDataUtil.standardize4AssembleProductEntityList(list);
        return assembleProductDao.updateBatch(list, corpid);
    }

    @Override
    public Integer deleteBatch(List<Long> productList, String corpid, Integer del) {
        if (CollectionsUtil.isEmpty(productList)) {
            return 0;
        }
        return assembleProductDao.deleteBatch(productList, corpid, del);
    }
}
